<#import "/layout/mainLayout.html" as mainLayout />

<#macro mainheading>
    <link href="/plugins/zTree/zTreeStyle.css"
          rel="stylesheet"/>
</#macro>
<#assign mainheading = mainheading in mainLayout />

<#macro maincontent>
    <section class="content">

        <div class="row">
            <div class="col-xs-12">
                <div class="box">

                    <div class="box-body table-responsive no-padding">
                        <div id="RightTreePannel"
                             class=" ztree" >

                        </div>

                    </div><!-- /.box-body -->

                </div><!-- /.box -->
            </div>
        </div>
    </section>

</#macro>
<#assign maincontent = maincontent in mainLayout />

<#macro mainfooter>

    <script src="/js/jquery/jquery.form.js"></script>
    <script src="/plugins/zTree/jquery.ztree.all-3.5.js"></script>
    <!--layoutmain footer...-->
    <!--<br />-->
    <script type="text/javascript">
        var zTreeObj;
        var Right_URL = [];
        var setting = {
            view: {
                selectedMulti: false,
                showLine: true,
                showText: true,
                showIcon: false,
                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom,
                fontCss: function (treeId, treeNode) {
                    if (treeNode.fParentid == 0) {
                        return {color: "blue"};
                    } else if (treeNode.fParentid == 1) {
                        return {color: "green"};
                    }
                }
            }
        }
        //加载列表
        LoadFuncList();
        function LoadFuncList() {

            $.ajax({
                    url: '/api/func/functree',
                    dataType: 'json',
                    success: function (data) {
                        if (mortise_layout.ValidataResult(data)) {
                            if (data.result) {
                                var node = {
                                    "fId": 0,
                                    "fName": "菜单列表",
                                    "fParentid": 0,
                                    "fDesc": "菜单列表",
                                    "fUrl": '',
                                    "fPic": '',
                                    "children": data.result
                                }
                                FillnodeName(node);
                                zTreeObj = $.fn.zTree.init($("#RightTreePannel"), setting, node);
                            }
                            else {
                                alert('返回数据错误！');
                            }
                        }
                        else {
                            alert('请求数据失败！');
                        }
                    }
                }
            );
        }
        function FillnodeName(node) {
            node.name = node.fName;
            node.tId = node.fId;
            //node.children = node.Children;
            node.title = node.fDesc;
            if (node.fId == 0) {
                node.open = true;
            } else {
                node.open = false;
            }
            Right_URL.push(node.fUrl);
            if (!node.children) {
                node.children = [];
            }
            if (node.children.length) {
                for (var i = 0; i < node.children.length; i++) {

                    FillnodeName(node.children[i]);
                }
            }

        }
        function nodeClick(event, treeId, treeNode) {

        }
        //为树的节点添加鼠标悬浮效果
        function addHoverDom(treeId, treeNode) {
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#diySpanStr_" + treeNode.tId).length > 0) return;
            var diySpanStr = "<span id='diySpanStr_" + treeNode.tId + "'>";
            if (treeNode.fParentid==0) {
                diySpanStr += "<span class='button add' id='addBtn_" + treeNode.tId +
                    "' title='添加子权限' "+
                    "onfocus='this.blur();'></span>";
            }

            if (treeNode.fId != 0){
                diySpanStr += "<span class='button edit' id='editBtn_" + treeNode.tId + "' title='修改权限' onfocus='this.blur();'></span>";
            }

//            if (treeNode.children && treeNode.children.length == 0) {
//                diySpanStr += "<span class='button remove' id='removeBtn_" + treeNode.tId + "' title='删除权限' onfocus='this.blur();'></span>";
//            }
            diySpanStr += "</span>";

            sObj.after(diySpanStr);
            //showFuncList(title, id, name, parentid, desc, url, pic)
            $('#addBtn_'+treeNode.tId).click(function () {
                showFuncList('添加菜单','','',treeNode.fId,'','','','');
            });
            $('#editBtn_'+treeNode.tId).click(function () {
                showFuncList('修改菜单',treeNode.fId,treeNode.fName,treeNode.fParentid,treeNode.fDesc,treeNode.fUrl,treeNode.fPic,treeNode.fShow);
            });

        }
        function initPaths(path) {
            $("#rightUrl").empty();
            $.ajax({
                url: '/adminapi/adminuser/GetAllAction',
                type: 'get',
                cache: true,
                success: function (result) {
                    if (!Adminlayout.ValidataResult(result)) {
                        return;
                    }
                    if (result._objectResult)
                        if (path) {
                            $("#rightUrl").append('<option value="' + path + '" selected>路径：' + path + '</option>');
                            $("#rightUrl").append('<option value="#" >路径：# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;请求方式：get</option>');
                        } else {
                            $("#rightUrl").append('<option value="#" selected>路径：# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;请求方式：get</option>');

                        }
                    for (var a = 0; a < result._objectResult.length; a++) {
                        var pathNode = result._objectResult[a];
                        for (var b = 0; b < pathNode.patterns.length; b++) {
                            if ($.inArray(pathNode.patterns[b], Right_URL) < 0) {
                                $("#rightUrl").append('<option value="' + pathNode.patterns[b] + '">路径：' + pathNode.patterns[b] + '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;请求方式：' + pathNode.methods.join() + '</option>');
                            }
                        }
                    }


                }

            });
        }

        ///url是否在存在树中
        function exsitUrl(url) {
            for (var node in Right_URL) {
                if (Right_URL[node] == url) {
                    return true;
                }
            }
            return false;
        }
        //为树的节点移除鼠标悬浮效果
        function removeHoverDom(treeId, treeNode) {
            $("#diySpanStr_" + treeNode.tId).unbind().remove();
        }


        function getNodePath(treeNode) {
            var tempnode = treeNode;
            var path = '/' + treeNode.fName;
            while (tempnode.getParentNode() != null) {
                tempnode = tempnode.getParentNode();
                path = '/' + tempnode.fName + path;
            }
            path = path + "/";
            return path;
        }


        function showFuncList(title, id, name, parentid, desc, url, pic,show) {
            if (pic==null){
                pic='';
            }
            if (url==null){
                url='';
            }
            if (pic==null){
                pic='';
            }
            //alert(id+'#'+name+'#'+parentid+'#'+desc+'#'+url+'#'+pic);
            var btnHtml = '';
            if (id) {
                btnHtml = '修改'
            }
            else {
                btnHtml = '添加'
            }
            var html =' <form id="funcform" class="form-horizontal" method="post"'+
                'enctype="multipart/form-data">'+
                '<div          class="form-horizontal">' +
                '<div class="box-body" style="min-width: 480px;">' +
                ' <input type="hidden" name="func_id" id="func_id" value="' + id + '">' +
                ' <input type="hidden" name="func_parentid" id="func_parentid" value="' + parentid + '">' +
                ' <div class="form-group">' +
                ' <label for="func_name" class="col-sm-3 control-label">名称</label>' +
                ' <div class="col-sm-9">' +
                ' <input type="text" class="form-control" ' +
                'id="func_name" placeholder="名称" value="'+name+'" name="func_name">' +
                ' </div>' +
                '  </div>' +
                ' <div class="form-group">' +
                ' <label for="func_url" class="col-sm-3 control-label">url连接:</label>' +
                ' <div class="col-sm-9">' +
                ' <input type="text" class="form-control" value="'+url+'" id="func_url" name="func_url" placeholder="url连接">' +
                '  </div>' +
                '  </div>' +

                '  <div class="form-group">' +
                ' <label for="func_pic" class="col-sm-3 control-label">样式名称</label>' +
                ' <div class="col-sm-9">' +
                '<input type="text" class="form-control" id="func_pic" value="'+pic+'" name="func_pic" placeholder="样式名称">' +
                '  </div>' +
                ' </div>' +
                '  <div class="form-group">' +
                ' <label for="func_show" class="col-sm-3 control-label">是否显示</label>' +
                ' <div class="col-sm-9">' +
                ' <select class="form-control" id="func_show" name="func_show">'+
                ' <option value="-1">--是否在菜单中显示--</option>' +
                ' <option value="0">显示</option>' +
                ' <option value="1">不显示</option>' +
                ' </select>' +
                '  </div>' +
                ' </div>' +


                '<div class="form-group">' +
                '<label for="func_desc" class="col-sm-3 control-label">' +
                '   描述</label>' +
                '   <div class="col-sm-9">' +
                '   <textarea class="form-control" name="func_desc" rows="3" placeholder="内容 ..."' +
                ' id="func_desc"></textarea>' +
                '  </div>' +
                '   </div>' +
                ' <div class="box-footer">' +
            '</div>' +
            '</div></form>';
            var postUrl='';
            if (id){
                postUrl='updatefunc'
            }
            else{
                postUrl='addfunc'
            }
            var d1 = dialog({
                title: title,
                content: html,
                cancel: true,
                okValue:'添加',
                ok:function () {
                    var options = {
                        url: '/api/func/'+postUrl,
                        type:'post',
                        success: function (data) {
                            if (data) {
                                if (data.code=='OperateOk') {
                                    dialog({
                                        title: '提示',
                                        content: '操作成功！',
                                        cancel: false,
                                        ok: function () {
                                        }
                                    }).width(320).showModal();
                                    LoadFuncList();
                                }
                                else {
                                    dialog({
                                        title: '提示',
                                        content: '操作失败！',
                                        cancel: false,
                                        ok: function () {
                                        }
                                    }).width(320).showModal();
                                }
                            }
                        }
                    };
                    $("#funcform").ajaxSubmit(options);
                }
            });
            d1.showModal().width(500).height(300);
            if (id&& desc){
                $('#func_desc').val(desc);
            }
            if (show){
                $('#func_show').val(show);
            }
        }


    </script>
</#macro>
<#assign mainfooter = mainfooter in mainLayout />
<@mainLayout.page title="用户管理"/>